package com.goods.business.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author jiaoyanlin
 * @Date 2021/11/8 12:28
 * @Description ProductCategoryServiceImpl
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    ProductCategoryMapper productCategoryMapper;

    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategories) {
            ProductCategoryTreeNodeVO treeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory, treeNodeVO);
            productCategoryTreeNodeVOList.add(treeNodeVO);
        }
        return productCategoryTreeNodeVOList;
    }

    @Override
    public PageVO<ProductCategoryTreeNodeVO> getCategoryList(Integer pageNum, Integer pageSize) {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = getProductCategoryTreeNodeVOS();
        List<ProductCategoryTreeNodeVO> voList = CategoryTreeBuilder.build(productCategoryTreeNodeVOList);
        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(voList, pageSize, pageNum);
        PageInfo<ProductCategoryTreeNodeVO> info = new PageInfo<>(page);
        return new PageVO<ProductCategoryTreeNodeVO>(info.getTotal(),voList);
    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = getProductCategoryTreeNodeVOS();

        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOList);

    }

    @Override
    public void add(ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {

          if (null!=productCategoryTreeNodeVO){
              ProductCategory productCategory = new ProductCategory();
              BeanUtils.copyProperties(productCategoryTreeNodeVO,productCategory);
              productCategoryMapper.insert(productCategory);
          }
        }

    @Override
    public ProductCategoryTreeNodeVO edit(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
        if (null!=productCategory){
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
        }
        return productCategoryTreeNodeVO;
    }

    @Override
    public void update(Long id, ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
          productCategoryTreeNodeVO.setId(id);
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryTreeNodeVO,productCategory);
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    //删除
    @Override
    public Boolean update(Long id) {
        //判断有没有子分类  如果有不能删
        //id 当做 pid   查出来下面子类 集合 如果空 可以删
//        Example o = new Example(ProductCategory.class);
//        Example.Criteria criteria = o.createCriteria();
//        criteria.andEqualTo("pid",id);
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        Long pid = productCategory.getPid();
        if (pid==0||pid==20){
            return false;
        }
        productCategoryMapper.deleteByPrimaryKey(id);
      return true;
        //TODO 判断有没有物质引用 如果有 不能删

    }


}